import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class Test {


    public static int demo(final List list, final int testCount) throws InterruptedException {
        ThreadGroup group = new ThreadGroup(list.getClass().getName() + "@" + list.hashCode());
        final Random rand = new Random();



        Runnable listAppender = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(rand.nextInt(2));
                } catch (InterruptedException e) {
                    return;
                }
                list.add("0");
            }
        };

        for (int i = 0; i < testCount; i++) {
            new Thread(group, listAppender, "InsertList-" + i).start();
        }

        while (group.activeCount() > 0) {
            Thread.sleep(10);
        }

        return list.size();
    }

    public static void main(String[] args) throws InterruptedException {
        hashTest();
        orderRepeatableTest();

    }
    // hash值测试
    public static void hashTest(){
        // 1的hash值是48
        for(int i = 0;i<9;i++){
            System.out.println(i+"的hashCode值:"+(i+"").hashCode());
        }
        //  a的hash值是97
        System.out.println("a的hashCode值:"+("a").hashCode());
        System.out.println("b的hashCode值:"+("b").hashCode());
        char a = 'a';

        // hash算法
        System.out.println((a*31+a));
        System.out.println(hash("aa"));
    }

    // hash值测试
    public static void BinaryStringTest(){
        // 无符号位移运算>>> 左乘右除
        System.out.println(2>>>1);

        // 负数绝对值取反+1
        System.out.println("Integer是"+Integer.toBinaryString(~2).length()+"位：");
        System.out.println("Integer最大值："+Integer.MAX_VALUE);
        System.out.println("Integer最大值："+Integer.MIN_VALUE);
        System.out.println("Integer最大值的二进制  ："+Integer.toBinaryString(Integer.MAX_VALUE));
        System.out.println("Integer最小值的二进制  ："+Integer.toBinaryString(Integer.MIN_VALUE));
        System.out.println("Integer最小值的二进制+1："+Integer.toBinaryString(Integer.MIN_VALUE+1));
        System.out.println("Integer最小值的二进制-1："+Integer.toBinaryString(Integer.MIN_VALUE-1));
        System.out.println("Integer最大值的二进制+1："+Integer.toBinaryString(Integer.MAX_VALUE+1));
        System.out.println("Integer最大值的二进制-1："+Integer.toBinaryString(Integer.MAX_VALUE-1));
        System.out.println("-2147483648的二进制："+Integer.toBinaryString(-2147483648));

        System.out.println("-2147483647的二进制："+Integer.toBinaryString(-2147483647));
        System.out.println("2147483648的二进制的二进制取反："+Integer.toBinaryString(~2147483647));
        System.out.println("2147483648的二进制取反+1："+Integer.toBinaryString(~2147483647+1));

        System.out.println("2的二进制："+Integer.toBinaryString(2));
        System.out.println("2的二进制取反："+Integer.toBinaryString(~2));
        System.out.println("2的二进制取反+1："+Integer.toBinaryString(~2+1));
        System.out.println("-2的二进制："+Integer.toBinaryString(-2));
        System.out.println(-2);

    }


    // list有序可重复 set无序不可重复
    public static void orderRepeatableTest(){
        List list = new ArrayList();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println(list.toString());
        Set<String> set = new HashSet<>();
        set.add("String2");
        set.add("String1");
        set.add("String4");
        set.add("String5");
        set.add("String3");

        set.forEach(e-> System.out.print(e+" "));
        System.out.println();


        //LinkedHashSet会保证元素的添加顺序
        Set<String> set2 = new LinkedHashSet<>();
        set2.add("String1");
        set2.add("String5");
        set2.add("String3");
        set2.add("String4");
        set2.add("String2");
        set2.forEach(e-> System.out.print(e+" "));
        System.out.println();


        //TreeSet保证元素自然顺序
        Set<String> set3 = new TreeSet<>();
        set3.add("String1");
        set3.add("String5");
        set3.add("String4");
        set3.add("String2");
        set3.add("String3");
        set3.forEach(e-> System.out.print(e+" "));
        System.out.println();
    }


    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }



}
